package org.jypj.dev.controller;

import org.jypj.dev.dao.ILogErrorDao;
import org.jypj.dev.model.LogError;
import org.jypj.dev.util.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ChenYu on 2017-04-25.
 */
@Controller
public class LogErrorController {


    @Resource
    private ILogErrorDao logErrorDao;

    /**
     * 跳转至错误日志页面
     *
     * @return
     */
    @RequestMapping(value = "error")
    public String toLogErrorList(HttpServletRequest request) {
        String todayString = DateUtils.getTodayString("yyyy-MM-dd");
        request.setAttribute("startDate", todayString);
        return "back/sys/log_error";
    }

    /**
     * 查询所有的错误日志记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectAllLogError")
    public Map<String, Object> selectAllLogError(String startDate, String searchKey) {
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        int code = 0;
        try {
            queryMap.put("startDate", startDate);
            queryMap.put("searchKey", searchKey);
            List<LogError> logErrors = logErrorDao.selectAllByMap(queryMap);
            dataMap.put("data", logErrors);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }

}
